package cn.ycc1.myupper.model;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * DeepSeek模型控制器
 * @author ycc
 * @date 2025/4/29
 */
@RestController
public class DeepSeekController {
//    private final DashScopeChatModel chatModel;
//
//    public DeepSeekController(DashScopeChatModel chatModel) {
//        this.chatModel = chatModel;
//    }

    private final ChatModel chatModel;

    public DeepSeekController(ChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/deepseek/chat/{prompt}")
    public Generation chat(@PathVariable(value = "prompt") String prompt) {

        ChatResponse chatResponse = chatModel.call(new Prompt(prompt));

        return chatResponse.getResult();
    }

    @GetMapping("/deepseek/reason/{prompt}")
    public String chatReasoning(@PathVariable(value = "prompt") String prompt) {

        ChatResponse chatResponse = chatModel.call(new Prompt(prompt));

        if (!chatResponse.getResults().isEmpty()) {
            Map<String, Object> metadata = chatResponse.getResults()
                    .get(0)
                    .getOutput()
                    .getMetadata();

            System.out.println(metadata.get("reasoningContent"));
        }

        return chatResponse.getResult().getOutput().getText();
    }
}
